package com.waiting.leetcode;

import java.util.*;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/3/6
 **/
public class LargestNumber179 {
    public static void main(String[] args) {
        int[] nums = {10, 2};
        System.out.println(largestNumber(nums));
    }

    public static String largestNumber(int[] nums) {
        if (nums == null) {
            return "";
        }
        if (nums.length < 1) {
            return String.valueOf(nums[0]);
        }
        List<Integer> tmp = new ArrayList<>(nums.length);
        for (int i = 0; i < nums.length; i++) {
            tmp.add(nums[i]);
        }
        Collections.sort(tmp, new Comparator<Integer>() {
            @Override
            public int compare(Integer a, Integer b) {
                String tmp = String.valueOf(a) + b;
                String tmpTwo = String.valueOf(b) + a;
                if (tmp.compareTo(tmpTwo) > 0) {
                    return -1;
                } else {
                    return 1;
                }
            }
        });
        StringBuilder stringBuilder = new StringBuilder();
        if (tmp.get(0) == 0) {
            return 0 + "";
        }
        for (int i = 0; i < nums.length; i++) {
            stringBuilder.append(tmp.get(i));
        }
        return stringBuilder.toString();
    }

    public int sort(int a, int b) {
        String tmp = String.valueOf(a) + b;
        String tmpTwo = String.valueOf(b) + a;
        return tmp.compareTo(tmpTwo);
    }
}
